package com.wskeee.ec.components.list
{
	import com.wskeee.ec.components.Image;
	import com.wskeee.ec.components.Label;
	import com.wskeee.ec.core.EComponent;
	import com.wskeee.ec.interfaces.IList;
	import com.wskeee.ec.interfaces.IListCellRenderer;
	
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	public class ListDefaultRenderer extends EComponent implements IListCellRenderer
	{
		public function ListDefaultRenderer()
		{
			super();
		}
		protected var isOver:Boolean = false;
		//========================= init ========================
		override protected function initEvent():void
		{
			super.initEvent();
			addEventListener(MouseEvent.ROLL_OVER,onRollOver);
			addEventListener(MouseEvent.ROLL_OUT,onRollOut);
		}
		
		
		override protected function initChildren():void
		{
			super.initChildren();
			initIcon();
			initLabel();
			initBackground();
		}
		
		protected function onRollOut(event:MouseEvent):void
		{
			if(isOver)
			{
				isOver = false;
				backgroundChangedFlag = true;
				invalidateProperties();
			}
		}
		
		protected function onRollOver(event:MouseEvent):void
		{
			if(!selected)
			{
				isOver = true;
				backgroundChangedFlag = true;
				invalidateProperties();
			}
		}
		
		protected function destoryEvent():void
		{
			removeEventListener(MouseEvent.ROLL_OVER,onRollOver);
			removeEventListener(MouseEvent.ROLL_OUT,onRollOut);
		}
		//========================= end =========================
		
		//========================= system render ===============
		override protected function commitProperties():void
		{
			super.commitProperties();
			if(labelChangedFlag)
			{
				labelChangedFlag = false;
				drawLabel();
				invalidateSize();
			}
			if(backgroundChangedFlag)
			{
				backgroundChangedFlag = false;
				drawBackgournd();
				invalidateDisplayList();
			}
		}
		override protected function measure():void
		{
			
		}
		override protected function updateDisplayList():void
		{
			super.updateDisplayList();
			icon.y = (height - icon.height)/2;
			label.x = icon.width + 5;
		}
		//========================= end =========================
		
		//========================= act ========================
		//---------------------
		//selected
		//---------------------
		private var _selected:Boolean;
		public function set selected(bo:Boolean):void
		{
			_selected = bo;
			invalidateProperties();
		}
		
		public function get selected():Boolean
		{
			return _selected;
		}
		
		//---------------------
		//list
		//---------------------
		private var _list:IList;
		public function get list():IList
		{
			return _list;
		}
		
		public function set list(value:IList):void
		{
			_list = value;
		}
		
		//---------------------
		//data
		//---------------------
		private var _data:Object;
		public function get data():Object
		{
			return _data;
		}
		
		public function set data(value:Object):void
		{
			_data = value;
			labelChangedFlag = true;
			backgroundChangedFlag = true;
			invalidateProperties();
		}
		
		//---------------------
		//listData
		//---------------------
		private var _listData:BaseListData;
		public function set listData(value:BaseListData):void
		{
			_listData = value;
			labelChangedFlag = true;
			backgroundChangedFlag = true;
			invalidateProperties();
		}
		
		public function get listData():BaseListData
		{
			return _listData;
		}
		
		
		override public function destory():void
		{
			_data = null;
			_list = null;
			destoryEvent();
			super.destory();
		}
		//========================= end =========================
		
		//========================= part ========================
		//---------------------
		//icon
		//---------------------
		protected var iconChangedFlag:Boolean = false;
		protected var icon:Image;
		protected function initIcon():void
		{
			icon = new Image();
			addChild(icon);
		}
		//---------------------
		//label
		//---------------------
		protected var labelChangedFlag:Boolean = true;
		protected var label:Label;
		protected function initLabel():void
		{
			label = new Label();
			label.mouseEnabled = label.mouseChildren = false;
			addChild(label);
		}
		protected function drawLabel():void
		{
			if(data)
			{
				label.text = listData ? listData.label : "";
				label.fontColor = getStyle("fontSelectedColor");
			}else
			{
				label.text = "";
			}
		}
		//---------------------
		//background
		//---------------------
		protected var backgroundChangedFlag:Boolean = true;
		protected var background:Sprite;
		protected function initBackground():void
		{
			background = new Sprite();
			addChildAt(background,0);
		}
		protected function drawBackgournd():void
		{
			if(!list)return;
			var color:uint = selected ? list.selectedColor as uint : isOver ? list.rolloverColor as uint : list.defaultColor as uint;
			var alpha:Number = selected ? list.selectedAlpha : isOver ? list.rolloverAlpha : list.defaultAlpha;
			var g:Graphics = background.graphics;
			g.clear();
			if(data)
			{
				g.beginFill(color,alpha);
				g.drawRect(0,0,width,height);
				g.endFill();
			}
		}
		//========================= end =========================
	}
}